﻿Imports AutoCAD
''' <summary>
''' 绘制孔服务
''' 1. 绘制垂直孔
''' 2. 绘制侧面水平孔
''' 3. 绘制立面水平孔
''' </summary>
Public Class CadDrawHoleServiceImpl
    Implements ICadDrawHoleService
    Private cadBlockReferenceService As ICadBlockReferenceService = New CadBlockReferenceServiceImpl
    Private cadDrawService As ICadDrawService = New CadDrawServiceImpl

    ''' <summary>
    ''' 绘制孔, 测试正面与反面无问题
    ''' </summary>
    ''' <param name="plate"></param>
    ''' <param name="px"></param>
    ''' <param name="py"></param>
    ''' <param name="pz"></param>
    ''' <param name="diameter"></param>
    ''' <param name="depth"></param>
    ''' <returns></returns>
    Public Function DrawHole_平面(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_平面
        Dim doc As AcadDocument = plate.Document
        doc.ActiveUCS = CadApplicationServiceImpl.WCS

        Dim ocs As AcadUCS = cadBlockReferenceService.GetOCS(plate)
        ocs.Origin = plate.InsertionPoint
        Dim 反转 As Double = IIf(cadBlockReferenceService.IsMirror(plate), -1, 1)

        Dim c = cadDrawService.DrawCircle(doc.ModelSpace, diameter)
        With c
            Dim p() As Double = New Double() {px * 反转, py, pz}
            c.Move(New Double() {0, 0, 0}, p)

            c.Thickness = depth
            c.TransformBy(ocs.GetUCSMatrix)
        End With

        Return c
    End Function

    ''' <summary>
    ''' 绘制孔, 测试侧面无问题
    ''' </summary>
    ''' <param name="plate"></param>
    ''' <param name="px"></param>
    ''' <param name="py"></param>
    ''' <param name="pz"></param>
    ''' <param name="diameter"></param>
    ''' <param name="depth"></param>
    ''' <returns></returns>
    Public Function DrawHole_侧面(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_侧面
        Dim doc As AcadDocument = plate.Document
        doc.ActiveUCS = CadApplicationServiceImpl.WCS

        Dim ocs As AcadUCS = cadBlockReferenceService.GetOCS(plate)
        ocs.Origin = plate.InsertionPoint
        Dim 反转 As Double = IIf(cadBlockReferenceService.IsMirror(plate), -1, 1)

        Dim c = cadDrawService.DrawCircle(doc.ModelSpace, diameter)
        With c
            .Normal = New Double() {1, 0, 0}
            Dim p() As Double = New Double() {px * 反转, py, pz}
            c.Move(New Double() {0, 0, 0}, p)
            c.Thickness = depth * 反转
            c.TransformBy(ocs.GetUCSMatrix)
        End With

        Return c
    End Function

    ''' <summary>
    ''' 绘制孔, 测试正反立面无问题
    ''' </summary>
    ''' <param name="plate"></param>
    ''' <param name="px"></param>
    ''' <param name="py"></param>
    ''' <param name="pz"></param>
    ''' <param name="diameter"></param>
    ''' <param name="depth"></param>
    ''' <returns></returns>
    Public Function DrawHole_立面(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_立面
        Dim doc As AcadDocument = plate.Document
        doc.ActiveUCS = CadApplicationServiceImpl.WCS

        Dim ocs As AcadUCS = cadBlockReferenceService.GetOCS(plate)
        ocs.Origin = plate.InsertionPoint
        Dim 反转 As Double = IIf(cadBlockReferenceService.IsMirror(plate), -1, 1)

        Dim c = cadDrawService.DrawCircle(doc.ModelSpace, diameter)
        With c
            .Normal = New Double() {0, 1, 0}
            Dim p() As Double = New Double() {px * 反转, py, pz}
            c.Move(New Double() {0, 0, 0}, p)

            c.Thickness = depth
            c.TransformBy(ocs.GetUCSMatrix)
        End With

        Return c
    End Function

    Public Function DrawHole(face As Integer, ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole

        'If (px > 700 And px < 800) Then
        '    MsgBox(766)
        'End If

        ' 获取当前文档
        Dim doc As AcadDocument = plate.Document

        ' 设置世界坐标系
        doc.ActiveUCS = CadApplicationServiceImpl.WCS

        ' 获取板件ocs坐标系
        Dim ocs As AcadUCS = cadBlockReferenceService.GetOCS(plate)
        ocs.Origin = plate.InsertionPoint
        Dim 反转 As Double = IIf(cadBlockReferenceService.IsMirror(plate), -1, 1) '判断是否为镜像板件
        Dim position() As Double = New Double() {px * 反转, py, pz} '孔相对于ocs的位置

        '241225:当前问题是当块镜像后, 5面孔的位置会发生改变
        '定义孔的法向量
        Dim normal() As Double
        Select Case face
            Case 0 '底面
                normal = New Double() {0, 0, 1}
                'position(0) = position(0) * 反转
            Case 1 '左侧面
                normal = New Double() {1, 0, 0}
                depth = depth * 反转
            Case 2 '前面
                normal = New Double() {0, 1, 0}
                depth = depth * 反转
            Case 3 '后面
                normal = New Double() {0, -1, 0}
                depth = depth * 反转
            Case 4 '右侧面
                normal = New Double() {-1, 0, 0}
                depth = depth * 反转
            Case 5 '顶面
                normal = New Double() {0, 0, -1}
                'position(0) = position(0) * 反转
            Case Else
                normal = New Double() {0, 0, 1}
        End Select

        ' 创建孔对象
        Dim c = cadDrawService.DrawCircle(doc.ModelSpace, diameter)

        With c
            .Normal = normal
            c.Move(New Double() {0, 0, 0}, position)
            c.Thickness = depth
            c.TransformBy(ocs.GetUCSMatrix)

        End With

        Return c
    End Function

    Public Function DrawHole_Face0(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face0
        Return DrawHole(0, plate, px, py, pz, diameter, depth)
    End Function
    Public Function DrawHole_Face1(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face1
        Return DrawHole(1, plate, px, py, pz, diameter, depth)
    End Function

    Public Function DrawHole_Face2(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face2
        Return DrawHole(2, plate, px, py, pz, diameter, depth)
    End Function

    Public Function DrawHole_Face3(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face3
        Return DrawHole(3, plate, px, py, pz, diameter, depth)
    End Function

    Public Function DrawHole_Face4(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face4
        Return DrawHole(4, plate, px, py, pz, diameter, depth)
    End Function

    Public Function DrawHole_Face5(ByRef plate As AcadBlockReference, px As Double, py As Double, pz As Double, diameter As Double, depth As Double) As Object Implements ICadDrawHoleService.DrawHole_Face5
        Return DrawHole(5, plate, px, py, pz, diameter, depth)
    End Function


End Class
